<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="wageth=device-wageth, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>

<body>
    <div id="root">
        <!-- 遍历数组（使用次数多） -->
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入名字" v-model="keyword">
        <button @click="sortType=1">升序</button>
        <button @click="sortType=2">降序</button>
        <button @click="sortType=0">源数据</button>
        <ul>
            <li v-for="(p,index) in filPersons" :key="p.id">{{p.name}}-{{p.gender}}-{{p.age}}
                <input type="text">
            </li>       
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false
        // 用compute实现
        var vm = new Vue({
            el: '#root',
            data: {
                keyword: '',
                sortType:'',
                persons: [{
                        id:'1',
                        age: '19',
                        name: "周杰伦",
                        gender: '男'
                    },
                    {
                        id:'2',
                        age: '20',
                        name: "马冬梅",
                        gender: '女'
                    }, {
                        id:'3',
                        age: '12',
                        name: "周冬雨",
                        gender: '女'
                    }, {
                        id:'4',
                        age: '11',
                        name: "温兆伦",
                        gender: '男'
                    }
                ],
            },
            computed:{
                // 计算属性在刚开始和所依赖的数据发生改变的时候调用
                filPersons(){
                    const arr = this.persons.filter((p)=>{
                        return p.name.indexOf(this.keyword) !==-1
                    })
                    if(this.sortType){
                        arr.sort((p1,p2)=>{
                            return this.sortType === 1? p1.age-p2.age: p2.age-p1.age
                        })
                    }
                    return arr
                },
            }
        })
    </script>
</body>

</html>